####################################
# Script Name: Eliminated Effect Interaction Plot
#
# Purpose: Create an interaction plot for the eliminated effect
#
# Author: Erica Ann Metheney
#
# Contact: data@gld.gu.se
#
######################################

# NECESSARY PACKAGES

library(haven)
library(ggpubr)
library(plyr)
library(interactions)
library(ggplot2)
library(grid)

# USER-DEFINED FUNCTIONS

get.int <- function(df,mediator.num){
  meds = c("BLANK","Competence","Distribution: Equally to all ethnic groups","Distribution: Equally to all villages","Observability")
  df = subset(df, Mediator %in% c("BLANK",meds[mediator.num]))
  fit = lm(vote ~ gender_at + ethnicity_at + origin_at + livingstatus_at + religion_at + age_at + class_at + Region + z + z*ethnicity_at, data = df)
  return(fit)
}

# DATA IMPORT

EVMB_CTRL <- read_dta("EVMB_CTRL.dta")
dat = rio::factorize(EVMB_CTRL)
dat$ethnicity_at = factor(dat$ethnicity_at,levels = c("Non-Co-Ethnic","Co-Ethnic"))# DATA CLEANING 
dat$ethnicity_at = mapvalues(dat$ethnicity_at, from = c("Non-Co-Ethnic","Co-Ethnic"),
                             to = c("No","Yes"))


# PLOT - COMPETENCY
dat$z = factor(as.character(dat$z),levels = c("0","1"))
dat$z = mapvalues(dat$z, from = c("0","1"), to = c("Information About\n Competency","No Information"))
dat$z = factor(dat$z, levels = c("No Information","Information About\n Competency"))

fit1 = get.int(df = dat,mediator = 2)
p1 <- cat_plot(fit1,pred = "z", modx = "ethnicity_at",legend.main = "Co-Ethnic with\nCandidate?",
               geom = "line",line.thickness = 0.5)+
  theme(text=element_text(size=12), 
        axis.text=element_text(size=12),
        axis.title=element_text(size=12), 
        plot.title=element_text(size=12), 
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))+
        ylab("Predicted Likelihood of Voting") +
        xlab("") + 
  ylim(0,1)+
  scale_color_manual("Co-Ethnic with\nCandidate?",values = c("#bdbdbd","#000000"))

# PLOT - EQUAL ETHNIC
dat$z = mapvalues(dat$z, from = c("No Information","Information About\n Competency"),
                  to = c("No Information","Equally Distribute\n to Ethnic Groups"))
fit2 = get.int(df = dat,mediator = 3)
p2 <- cat_plot(fit2,pred = "z", modx = "ethnicity_at",legend.main = "Co-Ethnic with\nCandidate?",
               geom = "line",line.thickness = 0.5)+ 
  theme(text=element_text(size=12), 
        axis.text=element_text(size=12),
        axis.title=element_text(size=12), 
        plot.title=element_text(size=12), 
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))+
  ylab("")+
  xlab("") + 
  ylim(0,1)+
  scale_color_manual("Co-Ethnic with\nCandidate?",values = c("#bdbdbd","#000000"))

# PLOT - EQUAL VILLAGE
dat$z = mapvalues(dat$z, from = c("No Information","Equally Distribute\n to Ethnic Groups"),
                  to = c("No Information","Equally Distribute\n to Villages"))
fit3 = get.int(df = dat,mediator = 5)
p3 <- cat_plot(fit3,pred = "z", modx = "ethnicity_at",legend.main = "Co-Ethnic with\nCandidate?",
               geom = "line",line.thickness = 0.5)+ 
  theme(text=element_text(size=12), 
        axis.text=element_text(size=12),
        axis.title=element_text(size=12), 
        plot.title=element_text(size=12), 
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))+
  ylab("Predicted Likelihood of Voting") +
  xlab("") + 
  ylim(0,1)+
  scale_color_manual("Co-Ethnic with\nCandidate?",values = c("#bdbdbd","#000000"))


# PLOT - OBSERVABILITY
dat$z = mapvalues(dat$z, from = c("No Information","Equally Distribute\n to Villages"),
                  to = c("No Information","Accessibility"))
fit4 = get.int(df = dat,mediator = 5)
p4 <- cat_plot(fit4,pred = "z", modx = "ethnicity_at",legend.main = "Co-Ethnic with\nCandidate?",
               geom = "line",line.thickness = 0.5)+ 
  theme(text=element_text(size=12), 
        axis.text=element_text(size=12),
        axis.title=element_text(size=12), 
        plot.title=element_text(size=12), 
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))+
  xlab("") +
  ylab("")+
  ylim(0,1)+
  scale_color_manual("Co-Ethnic with\nCandidate?",values = c("#bdbdbd","#000000"))


figure <- ggarrange(p1 + rremove("ylab") + rremove("xlab"), p2 + rremove("ylab") + rremove("xlab"),p3 + rremove("ylab") + rremove("xlab"), p4+ rremove("ylab") + rremove("xlab"), # remove axis labels from plots
                    labels = NULL,
                    ncol = 2, nrow = 2,
                    common.legend = TRUE, legend = "right",
                    align = "hv", 
                    font.label = list(size = 12, color = "black", face = "bold", family = NULL, position = "top"))

final.fig <- annotate_figure(figure, left = textGrob("Predicted Likelihood of Voting for the Candidate", rot = 90, vjust = 1, gp = gpar(cex = 1.1)),
                bottom = textGrob("Additional Information Provided", hjust = 0.75,gp = gpar(cex = 1.1)))

ggsave("EliminatedEffect_Interaction.png",plot = final.fig,dpi = 600)


